package com.ibiaoke.system.service;

import com.ibiaoke.common.base.Page;
import com.ibiaoke.common.exception.ServiceException;
import com.ibiaoke.common.utils.AssertUtils;
import com.ibiaoke.system.dao.AdvertDao;
import com.ibiaoke.system.entity.Advert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/6/12.
 */
@Service("advertService")
public class AdvertServiceImpl implements AdvertService {
    @Autowired
    private AdvertDao advertDao;

    /**
     * 添加Advert
     *
     * @param advert
     * @return
     */
    @Override
    public int saveAdvert(Advert advert) {

        AssertUtils.notNull(advert.getType(), "链接类型不能为空");
        if (advert.getType() == 1) {
            AssertUtils.notBlank(advert.getUrl(), "广告的链接地址不能为空");
        }
        if (advert.getType() == 2) {
            AssertUtils.notNull(advert.getObjectId(), "商品Id不能为空");
        }
        if (advert.getType() == 3) {
            AssertUtils.notNull(advert.getObjectId(), "分类Id不能为空");
        }
        AssertUtils.notBlank(advert.getImage(), "广告图片路径不能为空");
        AssertUtils.notNull(advert.getPosition(), "广告指定位置不能为空");

        advert.setShowFlag(1);
        advert.setDelStatus(0);
        advert.setCreateTime(new Date());
        advert.setUpdateTime(new Date());
        return advertDao.saveBanner(advert);
    }

    /**
     * 修改Advert
     *
     * @param advert
     * @return
     */
    @Override
    public int updateBannerById(Advert advert) {
        if (advert.getId() == null) {
            throw new ServiceException("广告编号不能为空");
        }
        advert.setUpdateTime(new Date());

        return advertDao.updateBannerById(advert);
    }

    /**
     * 删除Advert
     *
     * @param advertId
     * @return
     */
    @Override
    public int deleteBannerById(Integer advertId) {
        if (advertId == null) {
            throw new ServiceException("广告编号不能为空");
        }
        return advertDao.deleteBannerById(advertId);
    }

    /**
     * 查询Advert
     *
     * @param position
     * @param showFlag
     *@param page  @return
     */
    @Override
    public List<Advert> listBanner(Integer position, Integer showFlag, Page page) {
        if (position == null) {
            throw new ServiceException("指定位置不能为空");
        }
        return advertDao.listBanner(position,showFlag,page);
    }
}
